<template>
  <div class="d_mainFlex">
    <div class="d_treeBox p10">
      <el-form
        ref="materForm"
        :model="form"
        :rules="rules"
        hide-required-asterisk
        label-width="auto"
        class="dialogForm dialogInlineForm"
        label-position="top"
      >
        <el-row>
          <el-col>
            <el-form-item label="操作人员">
              <div class="flex">
                <el-input
                  v-model="name"
                  size="small"
                  placeholder="输入工号或扫码"
                  disabled
                />
                <el-button
                  size="small"
                  type="primary"
                  style="margin-left: 5px; min-width: 70px"
                  @click="loginModel = true"
                  >切换</el-button
                >
              </div>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="生产单号" prop="produceTaskBillNo">
              <el-input
                v-model="form.produceTaskBillNo"
                size="small"
                ref="produceTaskBillNo"
                @keyup.enter.native="handleSave"
                placeholder="输入生产单号或扫码"
              />
            </el-form-item>
          </el-col>
          <el-col>
            <el-button
              type="primary"
              size="small"
              style="width: 100%; margin-top: 10px"
              @click.native.prevent="handleSave"
              >确定</el-button
            >
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="case">
      <na-table
        :datas.sync="tableData"
        :searchObj.sync="search"
        @updateDatas="getAllMaterils"
        @newAdd="newAddClick"
      >
      </na-table>
      <SelectLLorder
        :per-visible.sync="showOrder"
        @current="selectCurrentRow"
      />
    </div>
    <!-- 人员寻找弹框   -->
    <per-redio-dialog
      :per-visible.sync="isperShow"
      @getEmployee="getEmployee"
    />
    <el-dialog
      :title="tname"
      :visible.sync="isAddProduct"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      width="85%"
      @close="closeDialog"
      @open="openCp"
    >
      <div class="dialogDody">
        <div class="addWuliaoBox">
          <el-form
            ref="ProductForm"
            :model="ProductForm"
            :rules="rules"
            label-width="110px"
            label-position="left"
            class="dialogForm dialogInlineForm"
            :disabled="canDiable"
          >
            <el-row>
              <el-col :span="8">
                <el-form-item label="退料单号" prop="backMaterialBillNo">
                  <el-input
                    :value="ProductForm.backMaterialBillNo || '系统自动生成'"
                    placeholder="退料单号"
                    size="mini"
                    disabled
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="创建人员" prop="createPersonName">
                  <el-input
                    :value="opsid != 1 ? ProductForm.createPersonName : name"
                    placeholder="创建人员"
                    size="mini"
                    disabled
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="创建日期" prop="createDate">
                  <el-input
                    :value="ProductForm.createDate || getDate()"
                    placeholder="创建日期"
                    size="mini"
                    disabled
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="订单单号" prop="saleOrderNo">
                  <el-input
                    v-model="ProductForm.saleOrderGroupNumber"
                    placeholder="订单单号"
                    size="mini"
                    disabled
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="协议书号" prop="xieYiShuHao">
                  <el-input
                    v-model="ProductForm.xieYiShuHao"
                    size="mini"
                    disabled
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="生产单号" prop="produceTaskBillNo">
                  <el-input
                    v-model="ProductForm.produceTaskBillNo"
                    size="mini"
                    disabled
                  >
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="产品名称" prop="orderProductName">
                  <el-input
                    v-model="ProductForm.orderProductName"
                    size="mini"
                    disabled
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="版面" prop="printingSurface">
                  <el-input
                    v-model="ProductForm.printingSurface"
                    size="mini"
                    disabled
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="版本" prop="productVersion">
                  <el-input
                    v-model="ProductForm.productVersion"
                    size="mini"
                    disabled
                  >
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="客户单号" prop="customerBillNo">
                  <el-input
                    v-model="ProductForm.customerBillNo"
                    size="mini"
                    disabled
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="退料类型" prop="backMaterialTypeName">
                  <el-input
                    v-model="ProductForm.backMaterialTypeName"
                    placeholder="生产退料"
                    size="mini"
                    disabled
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item label="退料备注" class="inlineText">
              <el-input
                v-model="ProductForm.backMaterialRemark"
                type="textarea"
                :rows="3"
                placeholder="退料备注"
                size="mini"
                clearable
              />
            </el-form-item>
            <el-form-item
              label="作废原因"
              class="inlineText mt10"
              v-if="ProductForm.cancelReason"
            >
              <el-input
                v-model="ProductForm.cancelReason"
                type="textarea"
                :rows="3"
                placeholder="作废原因"
                size="mini"
                clearable
              />
            </el-form-item>
          </el-form>
          <div class="tableBox">
            <div class="tt flex flex_between">
              <span class="fw">退料明细</span>
            </div>
            <div ref="bigdialogTable" class="bigdialogTbCon">
              <el-table
                :data="ProductForm.mxDataList"
                :row-key="(row) => row.rowKey"
                ref="detailTable"
                border
                style="width: 100%"
              >
                <el-table-column type="index" label="序号" width="50" />
                <el-table-column
                  prop="goodsCode"
                  width="150"
                  align="center"
                  label="物料编号"
                >
                  <template slot-scope="scope">
                    <div v-if="ops == 'add'">
                      {{ scope.row.stockGoodsCode || "--" }}
                    </div>
                    <div v-else-if="ops != 'add'">
                      {{ scope.row.goodsCode || "--" }}
                    </div>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="materialType"
                  width="100"
                  align="center"
                  label="物料类型"
                >
                  <template slot-scope="scope">
                    {{ scope.row.categoryName || "--" }}
                  </template>
                </el-table-column>
                <el-table-column
                  prop="school"
                  width="150"
                  align="center"
                  label="物料名称"
                >
                  <template slot-scope="scope">
                    {{ scope.row.goodsName || "--" }}
                  </template>
                </el-table-column>
                <el-table-column
                  prop="specificationCode"
                  width="150"
                  align="center"
                  label="规格型号"
                >
                  <template slot-scope="scope">
                    {{ scope.row.specificationName || "--" }}
                  </template>
                </el-table-column>
                <el-table-column
                  prop="equipmentName"
                  width="150"
                  align="center"
                  label="机台名称"
                >
                  <template slot-scope="scope">
                    {{ scope.row.equipmentName || "--" }}
                  </template>
                </el-table-column>
                <el-table-column
                  prop="backQuantity"
                  width="100"
                  align="center"
                  label="退料数量"
                >
                  <template slot-scope="{ row }">
                    <el-input
                      :disabled="canDiable"
                      v-model="row.backQuantity"
                      @input="$forceUpdate()"
                      placeholder="请输入"
                      size="mini"
                    />
                  </template>
                </el-table-column>
                <el-table-column
                  prop="goodsUnitName"
                  width="150"
                  align="center"
                  label="使用单位"
                >
                </el-table-column>
                <el-table-column prop="backRemark" align="center" label="备注">
                  <template slot-scope="{ row }">
                    <el-input
                      :disabled="canDiable"
                      v-model="row.backRemark"
                      placeholder="请输入"
                      size="mini"
                    />
                  </template>
                </el-table-column>
                <el-table-column
                  label="操作"
                  width="100px"
                  fixed="right"
                  align="center"
                  v-if="!canDiable"
                >
                  <template slot-scope="scope">
                    <el-button
                      type="text"
                      v-if="ProductForm.mxDataList.length > 1"
                      @click="handleDeleteRow(scope.row, scope.$index)"
                      >删除</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isAddProduct = false">取 消</el-button>
        <!-- 新增       -->
        <el-button
          v-if="opsid !== 2"
          :loading="addProductLoading"
          :disabled="addProductLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="savell"
          >{{ btm }}</el-button
        >
        <el-button
          v-if="opsid !== 2 && opsid !== 3"
          :loading="addProductLoading"
          :disabled="addProductLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="savell('confirm')"
          >保存并确认</el-button
        >
      </div>
    </el-dialog>
    <!-- 原因   -->
    <el-dialog
      title="作废原因"
      :visible.sync="isNopassshow"
      :close-on-click-modal="false"
      custom-class="comDialog"
      width="40%"
      @close="closePassDialog"
    >
      <div class="dialogDody">
        <el-form
          ref="passForm"
          :model="passForm"
          :rules="passFormRules"
          class="dialogForm"
        >
          <el-form-item class="mt12" prop="cancelReason">
            <el-input
              v-model="passForm.cancelReason"
              type="textarea"
              placeholder="请输入内容"
              :autosize="{ minRows: 6 }"
              size="mini"
            />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isNopassshow = false">取 消</el-button>
        <el-button
          :disabled="addLoading"
          element-loading-spinner="el-icon-loading"
          type="primary"
          size="mini"
          @click="passSave"
          >提交</el-button
        >
      </span>
    </el-dialog>

    <!-- 切换账号 -->
    <el-dialog
      title="快速登录"
      :visible.sync="loginModel"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog "
      width="25%"
    >
      <div class="p16 pt0">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          hide-required-asterisk
          label-width="auto"
          class="dialogForm dialogInlineForm"
          label-position="top"
        >
          <el-row>
            <el-col>
              <el-form-item label="登录账号" prop="loginAccount">
                <el-input
                  v-model="loginForm.loginAccount"
                  size="small"
                  placeholder="输入登录账号"
                />
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="登录密码" prop="loginPassWord">
                <el-input
                  v-model="loginForm.loginPassWord"
                  size="small"
                  show-password
                  placeholder="输入登录密码"
                />
              </el-form-item>
            </el-col>
            <el-col>
              <el-button
                type="primary"
                size="small"
                style="width: 100%; margin-top: 10px"
                @click.native.prevent="handleLogin"
                >确认登录</el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { arraySort } from "@/utils";
import HeadSeting from "@/components/HeadSeting";
import Pagination from "@/components/Pagination";
import perRedioDialog from "@/components/perDialogRedio";
import selectProductOrder from "@/components/selectProductOrder";
import SelectLLorder from "@/components/selectLLorder";
import screec from "@/components/screen";
import { mapGetters } from "vuex";
import naTable from "@/components/naTable";

export default {
  name: "Materiel",
  components: {
    Pagination,
    HeadSeting,
    perRedioDialog,
    selectProductOrder,
    SelectLLorder,
    screec,
    naTable,
  },
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码长度不正确"));
      } else {
        callback();
      }
    };
    return {
      ops: "add", // 操作、新建，修改，查看
      opsid: 1, // 3 确定 1 新建， 4 修改， 2 查看
      tableHeight: null, // 表格最大高度
      showOrder: false,
      isSeeMaterriel: false, // 查看弹框是否打开
      isAddMaterriel: false, // 新建弹框是否打开
      addMaterrielLoading: false, // 新建保存加载
      searchLoading: false, // 搜索加载效果
      allMaterirls: [], // 全部数据列表
      allMaterielClass: [], //机台
      colorState: null,
      addLoading: false,
      selectRow: null,
      isNopassshow: false,
      equipmentList: [],
      passForm: {
        cancelReason: "",
      },

      form: {
        produceTaskBillNo: "", //订单编号
        deviceCraftCode: "", //机台工艺编号
      },

      loginForm: {
        loginAccount: "", //账号
        loginPassWord: "", //密码
      },
      loginModel: false,

      loginRules: {
        loginAccount: [
          {
            required: true,
            trigger: "blur",
            message: "请输入账号",
          },
        ],
        loginPassWord: [
          {
            required: true,
            trigger: "blur",
            validator: validatePassword,
          },
        ],
      },

      // 表格操作
      ProductForm: {
        backMaterialType: 1,
        backMaterialPersonName: "",
        equipmentNo: "",
        equipmentName: "",
        mxDataList: [],
      },
      // 搜索条件
      search: {
        QueryDataBaseFiled: "",
        pageIndex: 1,
        pageSize: 30,
        likeWords: "",
      },

      passFormRules: {
        cancelReason: [
          { required: true, message: "请输入作废原因", trigger: "blur" },
        ],
      },

      tableData: {
        headId: "head", //表头列显隐组件所需ID-避免多个页面id重复问题
        tableId: "backMaterialID", //表格列表key,也就是一条数据的唯一标识
        senior: false, //高级搜索是否显示
        addPermission: "", //新建按钮权限
        addTitle: "", //新建按钮名称-为''则不显示，为undefine则为新建
        disableWord: "", //是否禁用显示的字段
        disableStatus: "", //判断未禁用的状态值
        //表格按钮
        operationBtn: [
          {
            title: "查看",
            methodsName: "handleClick",
          },
          {
            title: "编辑",
            methodsName: "handleUpdata",
            permission: "MenuCode2-4-2",
            hideCondition: {
              //按钮显隐条件
              prop: "backMaterialBillState", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用
                0,
              ],
            },
          },
          {
            title: "确定",
            methodsName: "handleConfirm",
            permission: "MenuCode2-4-3",
            hideCondition: {
              //按钮显隐条件
              prop: "backMaterialBillState", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用
                0,
              ],
            },
          },
          {
            title: "作废",
            methodsName: "handleDone",
            permission: "MenuCode2-4-5",
            hideCondition: {
              //按钮显隐条件
              prop: "backMaterialBillState", //控制显隐的状态字段
              show: [
                //有哪些状态可以使用
                0,
              ],
            },
          },
          {
            title: "打印退料单", //按钮名称
            methodsName: "handleDownload", //按钮绑定方法的字段
          },
          // {
          //   title: "复制",
          //   methodsName: "handleCopy",
          //   permission: "MenuCode2-4-6",
          //   hideCondition: {
          //     //按钮显隐条件
          //     prop: "backMaterialBillState", //控制显隐的状态字段
          //     show: [
          //       //有哪些状态可以使用
          //       2,
          //     ],
          //   },
          // },
          // {
          //   title: "删除",
          //   methodsName: "handleDelete",
          //   permission: "MenuCode2-4-4",
          //   hideCondition: {
          //     //按钮显隐条件
          //     prop: "backMaterialBillState", //控制显隐的状态字段
          //     show: [
          //       //有哪些状态可以使用
          //       0,
          //     ],
          //   },
          // },
        ],
        //表格内容
        tbody: [],
        //表格表头
        thead: [],
      },

      total: 0, // 总条数
      tableHead: [], // 表格表头
      addProductLoading: false,
      isAddProduct: false,
      isperShow: false,
      multipleSelection: null,
      purchaseInfoDatas: [{}],
      purchaseTypes: [
        {
          dicValueID: 0,
          dicValueDesc: "生产领料",
          dicValueCode: 1,
        },
        {
          dicValueID: 1,
          dicValueDesc: "生产补料",
          dicValueCode: 2,
        },
      ],
      SemiSearch: [],
      materialList: [],
      marLoading: false,
      canDiable: false,
      rules: {
        backMaterialPersonName: [
          { required: true, message: "请选择人员", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["name", "userInfo"]),
    btm() {
      if (this.opsid === 3) {
        return "确认";
      } else {
        return "仅保存";
      }
    },
    tname() {
      if (this.opsid === 1) {
        return "新建生产退料单";
      } else if (this.opsid === 2) {
        return "查看生产退料单";
      } else if (this.opsid === 3) {
        return "确定生产退料单";
      } else if (this.opsid === 4) {
        return "修改生产退料单";
      } else {
        return "复制生产退料单";
      }
    },
  },
  created() {
    //  所有列表数据
    this.getAllMaterils();
    // 表格得表头
    this.getTableHead();

    this.getMaterielClass();
  },
  mounted() {},
  updated() {},
  methods: {
    closePassDialog() {
      this.passForm.cancelReason = "";
      this.selectRow = null;
    },

    //删除
    handleDeleteRow(row, index) {
      this.ProductForm.mxDataList.splice(index, 1);
    },

    //点击确定
    handleSave() {
      this.$refs.materForm.validate((valid) => {
        if (valid) {
          this.$api.product
            .GetProduceTaskBillDetail({
              produceTaskBillNo: this.form.produceTaskBillNo,
            })
            .then((res) => {
              this.$api.product.GetOutStockDetailViewList({
                associationBillNo: this.form.produceTaskBillNo,
	              requestType: 1
              }).then((data)=>{
                this.materialList = data;
                this.ProductForm.mxDataList = data;
                this.ProductForm.mxDataList.forEach((item)=>{
                  item.stockGoodsCode = item.specificationCode
                  item.equipmentName = item.machineName
                })
              })

              this.ops = "add";
              this.canDiable = false;
              this.opsid = 1;
              this.ProductForm.backMaterialTypeName = "生产退料";
              this.isAddProduct = true;
              // this.ProductForm.saleOrderNo =
              //   res.orderBaseData.saleOrderGroupNumber;

              this.ProductForm.saleOrderGroupNumber =
                res.orderBaseData.saleOrderGroupNumber;
              this.ProductForm.saleOrderNo = res.orderBaseData.orderNo;
              this.ProductForm.productCode = res.baseData.productCode;
              this.ProductForm.produceTaskBillNo =
                res.baseData.produceTaskBillNo;
              this.ProductForm.customerBillNo =
                res.orderBaseData.customerBillNo;
              this.ProductForm.customerCode =
                res.orderBaseData.customerCode;
              this.ProductForm.orderProductName =
                res.productInfo.baseData.productName;
              this.ProductForm.printingSurface =
                res.productInfo.baseData.productLayout;
              this.ProductForm.productVersion =
                "V" +
                res.productInfo.baseData.newVersion +
                "." +
                res.productInfo.baseData.updateVersion;
              this.ProductForm.xieYiShuHao = res.orderBaseData.xieYiShuHao;
              // this.materialList = res.produceMaterialList;
              // this.ProductForm.mxDataList = res.produceMaterialList;
              // if (this.ProductForm.mxDataList.length) {
              //   this.ProductForm.mxDataList.forEach((item) => {
              //     item.machineCode = item.equipmentNo;
              //   });
              // }
              // this.ProductForm.mxDataList.push({
              //   produceMaterialNo: "",
              //   stockGoodsCode: "",
              //   applyGetQuantity: 0,
              //   getMaterialRemark: "",
              //   machineCode: "",
              //   goodsCode: "",
              //   supplierCode: "",
              //   materialList: [],
              //   specificationCode: "",
              // });
            });
        }
      });
    },

    //登录
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              // this.$router.push({ path: '/scanning/code' })
              location.reload();
              // location.reload(true)
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },

    // 获取所有设备
    getMaterielClass() {
      this.$api.product
        .GetMatchProductDevices({
          likeWords: "",
          machineState: 1,
        })
        .then((res) => {
          this.allMaterielClass = res;
        });
    },

    //下载退料单
    handleDownload(row) {
      let parmas = {
        templateTypeCode: "MBLX0013", //	模板类型编号
        customerCode: row.customerCode,
        condition: {
          BackMaterialBillNo: row.backMaterialBillNo,
        }, //查询条件
      };
      this.$api.system.DownLoadTemplate(parmas).then((res) => {
        window.open(res);
      });
    },

    checkProductList(row) {
      this.$router.push({
        path: "/business/orderDetail", // 跳转的路径
        query: {
          tabIndex: 5,
          saleOrderNo: row.saleOrderNo,
        },
      });
    },
    // 新建点击事件
    newAddClick() {
      this.ops = "add";
      // this.isAddProduct = true
      this.opsid = 1;
      this.showOrder = true;
      this.canDiable = false;
      this.ProductForm.backMaterialPersonName = this.userInfo.employeeName;
      this.ProductForm.backMaterialPerson = this.userInfo.employeeCode;
    },
    handleClick(row) {
      this.opsid = 2;
      this.ops = "see";
      this.canDiable = true;
      this.getDetail(row);
    },
    handleConfirm(row) {
      this.opsid = 3;
      this.canDiable = true;
      this.getDetail(row);
    },
    handleDone(row) {
      this.selectRow = row;
      this.isNopassshow = true;
    },
    handleCopy(row) {
      this.opsid = 5;
      this.canDiable = false;
      // this.getDetail(row)
      this.openCp();

      const _data = {
        backMaterialID: row.backMaterialID,
        backMaterialBillNo: row.backMaterialBillNo,
      };
      this.$api.product.GetBackProduceMaterialBillDetail(_data).then((res) => {
        this.equipmentList.push({
          equipmentName: res.baseData.equipmentName,
          equipmentNo: res.baseData.equipmentNo,
        });
        delete res.baseData.backMaterialID;
        delete res.baseData.backMaterialBillNo;
        this.ProductForm = res.baseData;
        this.ProductForm.mxDataList = res.mxDataList;
        this.isAddProduct = true;
      });
    },
    handleUpdata(row) {
      this.opsid = 4;
      this.canDiable = false;
      this.getDetail(row);
    },
    passSave() {
      this.$refs.passForm
        .validate((valid) => {
          if (valid) {
            const _data = {
              backMaterialID: this.selectRow.backMaterialID,
              cancelReason: this.passForm.cancelReason,
            };
            this.$api.product.CancelBackProduceMaterialBill(_data).then(() => {
              this.getAllMaterils();
              this.isNopassshow = false;
              this.addLoading = false;
              this.$message({
                message: "作废成功",
                type: "success",
              });
            });
          }
        })
        .catch(() => {
          this.isNopassshow = false;
          this.addLoading = false;
        });
    },
    handleDelete(row) {
      const _data = {
        backMaterialID: row.backMaterialID,
      };
      this.$confirm("确定删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$api.product
          .DeleteBackProduceMaterialBill(_data)
          .then(() => {
            this.getAllMaterils();
            this.$message({
              message: "删除成功",
              type: "success",
            });
          })
          .catch(() => {});
      });
    },

    getDate() {
      return this.dayjs().format("YYYY-MM-DD");
    },
    getDetail(row) {
      const _data = {
        backMaterialID: row.backMaterialID,
        backMaterialBillNo: row.backMaterialBillNo,
      };
      this.$api.product.GetBackProduceMaterialBillDetail(_data).then((res) => {
        this.equipmentList.push({
          equipmentName: res.baseData.equipmentName,
          equipmentNo: res.baseData.equipmentNo,
        });
        this.ProductForm = res.baseData;
        this.ProductForm.orderProductName = res.baseData.productName;
        this.ProductForm.productVersion = res.baseData.productVersion;
        this.ProductForm.printingSurface = res.baseData.productLayout;
        this.ProductForm.saleOrderGroupNumber = res.baseData.saleOrderNo;
        this.ProductForm.customerCode = res.baseData.customerCode;
        if (res.baseData.backProduceMaterialMxList.length) {
          res.baseData.backProduceMaterialMxList.forEach((item) => {
            item.categoryName = item.goodsType;
            item.backQuantity = item.quantity;
            item.goodsUnitName = item.goodsUnit;
            item.equipmentName = item.machineName;
          });
        }
        this.$set(
          this.ProductForm,
          "mxDataList",
          res.baseData.backProduceMaterialMxList
        );

        // this.ProductForm.mxDataList = res.baseData.backProduceMaterialMxList;
        this.isAddProduct = true;
      });
    },
    backQuantityInput(row) {
      if (Number(row.backQuantity) > Number(row.receiveQuantity)) {
        this.$message.warning("退料数量不能大于领料数量");
        row.backQuantity = row.receiveQuantity;
      }
    },
    selectCurrentRow(row) {
      console.log(row);
      this.ops = "add";
      this.isAddProduct = true;
      this.ProductForm.getMaterialBillNo = row.getMaterialBillNo;
      this.ProductForm.backMaterialTypeName = "生产退料";
      this.ProductForm.equipmentName = row.equipmentName;
      this.ProductForm.produceTaskBillNo = row.produceTaskBillNo;
      this.ProductForm.saleOrderNo = row.saleOrderNo;
      this.ProductForm.productVersion =
        row.printingSurface + row.productVersion;
      this.ProductForm.productCode = row.productCode;
      this.ProductForm.customerBillNo = row.customerBillNo;
      this.ProductForm.productCode = row.productCode;
      this.ProductForm.orderProductName = row.orderProductName;
    },

    openCp() {
      const _data = {
        getMaterialBillNo: this.ProductForm.getMaterialBillNo,
        likeWords: "",
      };
      this.$api.product.GetProduceTaskEquipmentList(_data).then((res) => {
        this.equipmentList = res;
        if (res && res.length) {
          this.ProductForm.equipmentNo = res[0].equipmentNo;
        }
      });
    },

    savell(type) {
      if (this.opsid === 3) {
        const _data3 = {
          backMaterialID: this.ProductForm.backMaterialID,
        };
        this.$api.product
          .SureBackProduceMaterialBill(_data3)
          .then(() => {
            // let parmas = {
            //   templateTypeCode: "MBLX0013", //	模板类型编号
            //   customerCode: this.ProductForm.customerCode,
            //   condition: {
            //     BackMaterialBillNo: this.ProductForm.backMaterialBillNo,
            //   }, //查询条件
            // };
            // this.$api.system.DownLoadTemplate(parmas).then((res) => {
            //   window.open(res);
            // });
            this.addProductLoading = false;
            this.isAddProduct = false;
            this.getAllMaterils();
            this.$message({
              message: "确认成功",
              type: "success",
            });
          })
          .catch(() => {
            this.addProductLoading = false;
          });
      } else if (this.opsid === 4) {
        const parmas = this.ProductForm;
        this.$refs.ProductForm.validate((valid) => {
          if (valid) {
            this.addProductLoading = true;
            if (type === "confirm") {
              this.$api.product
                .UpdateBackProduceMaterialBill(parmas)
                .then(() => {
                  const _data3 = {
                    backMaterialID: this.ProductForm.backMaterialID,
                  };
                  this.$api.product
                    .SureBackProduceMaterialBill(_data3)
                    .then(() => {
                      // let parmas = {
                      //     templateTypeCode: "MBLX0013", //	模板类型编号
                      //     customerCode: this.ProductForm.customerCode,
                      //     condition: {
                      //       BackMaterialBillNo: this.ProductForm.backMaterialBillNo,
                      //     }, //查询条件
                      //   };
                      //   this.$api.system.DownLoadTemplate(parmas).then((res) => {
                      //     window.open(res);
                      //   });
                      this.addProductLoading = false;
                      this.isAddProduct = false;
                      this.getAllMaterils();
                      this.$message({
                        message: "确认成功",
                        type: "success",
                      });
                    })
                    .catch(() => {
                      this.addProductLoading = false;
                    });
                })
                .catch(() => {
                  this.addProductLoading = false;
                });
            } else {
              this.$api.product
                .UpdateBackProduceMaterialBill(parmas)
                .then(() => {
                  this.addProductLoading = false;
                  this.isAddProduct = false;
                  this.getAllMaterils();
                  this.$message({
                    message: "修改成功",
                    type: "success",
                  });
                })
                .catch(() => {
                  this.addProductLoading = false;
                });
            }
          }
        });
      } else {
        const parmas = this.ProductForm;
        this.$refs.ProductForm.validate((valid) => {
          if (valid) {
            this.addProductLoading = true;
            if (type === "confirm") {
              this.$api.product
                .CreateBackProduceMaterialBill(parmas)
                .then((res) => {
                  const _data3 = {
                    backMaterialID: res.backMaterialID,
                  };
                  this.$api.product
                    .SureBackProduceMaterialBill(_data3)
                    .then(() => {
                      // let parmas = {
                      //     templateTypeCode: "MBLX0013", //	模板类型编号
                      //     customerCode: this.ProductForm.customerCode,
                      //     condition: {
                      //       BackMaterialBillNo: res.backMaterialBillNo,
                      //     }, //查询条件
                      //   };
                      //   this.$api.system.DownLoadTemplate(parmas).then((ress) => {
                      //     window.open(ress);
                      //   });
                      this.addProductLoading = false;
                      this.isAddProduct = false;
                      this.getAllMaterils();
                      this.$message({
                        message: "确认成功",
                        type: "success",
                      });
                    })
                    .catch(() => {
                      this.addProductLoading = false;
                    });
                })
                .catch(() => {
                  this.addProductLoading = false;
                });
            } else {
              this.$api.product
                .CreateBackProduceMaterialBill(parmas)
                .then((res) => {
                  this.addProductLoading = false;
                  this.isAddProduct = false;
                  this.getAllMaterils();
                  this.$message({
                    message: "保存成功",
                    type: "success",
                  });
                })
                .catch(() => {
                  this.addProductLoading = false;
                });
            }
          }
        });
      }
    },

    // 得到申请人员
    getEmployee(val) {
      this.ProductForm.backMaterialPersonName = val.employeeName;
      this.ProductForm.getMaterialPerson = val.employeeCode;
    },

    // 获取表格表头
    getTableHead() {
      const parmas = {
        tableCode: this.$config.producebackmaterialbilllist,
      };
      this.$api.common.getAllTableHead(parmas).then((res) => {
        res.sort(arraySort("columnSort"));
        this.tableData.thead = res;
      });
    },
    // 所有列表数据
    getAllMaterils() {
      const parmas = this.search;
      this.searchLoading = true;
      this.$api.product
        .GetMatchPageBackProduceMaterialBills(parmas)
        .then((res) => {
          this.allMaterirls = res.list;
          this.tableData.tbody = res.list;
          this.search.total = res.totalCount;
          this.total = res.totalCount;
          this.searchLoading = false;
        })
        .catch(() => {
          this.searchLoading = false;
        });
    },
    // 新建弹框关闭事件
    closeDialog() {
      if (this.$refs.ProductForm) {
        this.$refs.ProductForm.resetFields();
      }
      this.ProductForm = this.$options.data().ProductForm;
    },
  },
};
</script>
<style lang="scss" scoped>
.container {
  height: 100%;
}
.permangeRg {
  border: 1px solid #e6e6e6;
  background: #fff;
  height: 100%;

  .mainTT {
    font-size: 16px;
    font-weight: bold;
    width: 100%;
    padding: 18px 16px;
    padding-bottom: 0;
  }
}
.addWuliaoBox {
  padding: 10px 16px;

  .tableBox {
    margin-top: 16px;
    border: 1px solid #e6e6e6;
    border-bottom: none;
    border-radius: 8px 8px 0 0;
    overflow: hidden;

    .tt {
      height: 38px;
      line-height: 38px;
      padding-left: 16px;
      background: #f7f8fa;
      border-radius: 8px 8px 0 0;
    }
  }
}

.subTip {
  border-bottom: 1px #e6e6e6 solid;
  background: #f5f6f9;
  padding: 8px 16px;
}
</style>
